<?php

//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                         |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: songaimin@clcw.com.cn  Blog:http://blog.csdn.net/samxx8        |
// +------------------------------------------------------------------------+
//

namespace AuctionCore\Other;

use AuctionCore\AntNest\UserCenter;
use AuctionCore\CarDealer\Dealer;
use AuctionCore\System\AuctionException;

/**
 * 车商投诉建议功能类
 * @author  Eric Lee
 */
class DealerFeedback extends Feedback {

    /**
     * 新反馈
     * @param    int $data
     * @return   Object
     */
    public function add($data = array()) {
        if (empty($data['desc'])) {
            AuctionException::make_throw('800003');
        }
        $data['ip'] = get_client_ip();
        $data['createtime'] = date('Y-m-d H:i:s');
        return M('dealer_feedback')->add($data);
    }

    /**
     * 获取反馈列表
     * 字段展示：地址，公告标题，日期
     * @param    array $map    条件
     * @param    int $limitRows 每页记录记录数
     * @return   Array
     */
    public function get_list($map = array('status' => array('gt', '-1')), $limitRows = '20') {
        //计算记录数
        $total = M('dealer_feedback')->where($map)->count();
        $p = new \AuctionCore\Common\AjaxPage($total, $limitRows, "get_list"); //第三个参数是需要调用换页的ajax函数名
        $p->setConfig('first', '首页');
        $p->setConfig('last', '末页');
        $p->setConfig('theme', '  %first%   %upPage%  %linkPage% %prePage% %downPage% %nextPage% %end% %jumpform%');
        $limit_value = $p->firstRow . "," . $p->listRows;
        $res = M('dealer_feedback')->where($map)->order("df_id DESC")->limit($limit_value)->select();
        $list = array();
        foreach ($res as $key => $value) {
            $list[$key] = $value;
            $list[$key]['createtime'] = date('Y-m-d', strtotime($value['createtime']));
        }
        if (I('get.ajax')) {
            echo json_encode(array('list' => $list, 'page' => $p->show()), true);
            exit;
        }
        return array('list' => $list, 'page' => $p->show());
    }

    /**
     * 投诉与建议列表
     * 
     * @return Array
     */
    public function dealer_feedback_list($page, $limit, $params) {
        $page = (int) $page < 1 ? 1 : (int) $page;
        $limit = (int) $limit < 1 ? 10 : (int) $limit;
        //搜索 
        $map = array();
        //投诉或建议类型
         if ($params['start_time'] && !$params['end_time']) {
            $map['df.createtime'] = array('egt', $params['start_time']." 00:00:00");
        }elseif ($params['end_time'] && !$params['start_time']) {
            $map['df.createtime'] = array('elt', $params['end_time']." 23:59:59");
        }elseif ($params['start_time'] && $params['end_time']) {
            $map['df.createtime'] = array(array('egt', $params['start_time']." 00:00:00"),array('elt', $params['end_time']." 23:59:59"));
        }
        if (!empty($params['feedback_type'])) {
            $map['df.type'] = array('eq', $params['feedback_type']);
        }
        if ($params['status']!="") {
            $map['df.status'] = array('eq', $params['status']);
        }
        if (!empty($params['keyword'])) {
            $where = array();
            if(is_numeric($params['keyword'])) {
                $uc = new UserCenter();
                $info1 = $uc->info_bymobile($params['keyword'], 2, 1);
                $where['du.uid'] = array('eq', $info1['account_id']);
            }else{
                $where['cd.contact_person'] = array('like', '%' . $params['keyword'] . '%');
            }
            $map['_complex'] = $where;
        }
        //要查询的列
        $cols = "df.df_id,df.dealer_id,df.desc,df.mark,df.type,df.replytime,df.comefrom,df.status,df.ip,df.createtime,cd.fullname,cd.contact_person,du.uid,emp.username,u.user_name";
        $total = M('DealerFeedback')->alias('df')->field($cols)
                        ->join(C('DB_PREFIX') . "car_dealer as cd on df.`dealer_id`=cd.`dealer_id`")
                        ->join(" left join ".C('DB_PREFIX') . "dealer_user as du on du.`dealer_id`= df.`dealer_id`")
                        ->join(" left join ".C('DB_PREFIX') . "user as u on u.`uid`= du.`uid`")
                        ->where($map)->count();
        $list = M('DealerFeedback')->alias('df')->field($cols)
                        ->join(C('DB_PREFIX') . "car_dealer as cd on df.`dealer_id`=cd.`dealer_id`")
                        ->join(" left join ".C('DB_PREFIX') . "employee as emp on df.`emp_id`=emp.`emp_id`")
                        ->join(" left join ".C('DB_PREFIX') . "dealer_user as du on du.`dealer_id`= df.`dealer_id`")
                        ->join(" left join ".C('DB_PREFIX') . "user as u on u.`uid`= du.`uid`")
                        ->where($map)->order("createtime desc")->limit($limit)->page($page)->select();
        if(!empty($list)){
            $dealer = new Dealer();
            $list= $dealer->get_contact_mobile_byAntNest($list);
        }
        $result = array(
            'list' => $list,
            'total' => $total
        );
        return $result;
    }

    /**
     * 投诉与建议列表 用于app
     * 
     * @return Array
     */
    public function app_dealer_feedback_list($page, $limit, $params) {
        $page = (int) $page < 1 ? 1 : (int) $page;
        $limit = (int) $limit < 1 ? 10 : (int) $limit;
        //搜索 
        $map = array();
        //投诉或建议类型
        if (!empty($params['type'])) {
            $map['df.type'] = array('eq', $params['type']);
        }
        $map = $params;
        //要查询的列
        $cols = "df.df_id,df.dealer_id,df.desc,df.mark,df.type,df.comefrom,df.status,df.ip,df.createtime,cd.fullname,cd.contact_person,cd.contact_mobile";
        $total = M('DealerFeedback')->alias('df')->field($cols)
                        ->join(C('DB_PREFIX') . "car_dealer as cd on df.`dealer_id`=cd.`dealer_id`")
                        ->where($map)->count();
        $list = M('DealerFeedback')->alias('df')->field($cols)
                        ->join(C('DB_PREFIX') . "car_dealer as cd on df.`dealer_id`=cd.`dealer_id`")
                        ->where($map)->limit($limit)->page($page)->order("df.`df_id` DESC")->select();
//        echo M('DealerFeedback')->getLastSql();
        $feedback_list = array();
        foreach ($list as $k => $v) {
            $feedback_list[$k]['type'] = $v['type'];
            $feedback_list[$k]['createtime'] = $v['createtime'];
            $feedback_list[$k]['desc'] = $v['desc'];
            $feedback_list[$k]['reply'] = $v['mark'];
        }
        $result = array(
            'currentpage' => $page,
            'pagesize' => $limit,
            'maxcount' => $total,
            'maxpage' => ceil($total / $limit),
            'list' => $feedback_list
        );
        return $result;
    }

    /**
     * 删除反馈
     * @param    int $df_id    反馈ID
     * @return int
     */
    public function del($df_id) {
        $data = array('status' => '-1');
        return M('dealer_feedback')->where("df_id='" . $df_id . "'")->save($data);
    }

}
?>
